package com.zhiche.lisa.bms.service.statistics.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.mapper.statistics.StaticWaybillMapper;
import com.zhiche.lisa.bms.dao.model.statistics.StaticWaybill;
import com.zhiche.lisa.bms.dao.model.statistics.StaticWaybillComposite;
import com.zhiche.lisa.bms.service.statistics.StaticWaybillCompositeService;
import com.zhiche.lisa.bms.service.statistics.StaticWaybillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 运单统计表 服务实现类
 * </p>
 *
 * @author liangpeng
 * @since 2019-01-18
 */
@Service
public class StaticWaybillServiceImpl extends ServiceImpl<StaticWaybillMapper, StaticWaybill> implements StaticWaybillService {

    @Autowired
    private StaticWaybillCompositeService waybillCompositeService;


    @Override
    public List<StaticWaybill> getStaticWayBillByOrder(int intCount) {
        EntityWrapper<StaticWaybill> ewStaticWaybill = new EntityWrapper<>();
        ewStaticWaybill.eq("deal_status", 0);
        ewStaticWaybill.orderBy("gmt_modify");
        if (intCount > 0) {
            ewStaticWaybill.last("limit " + intCount);
        }
        return baseMapper.getStaticWayBillByOrder(ewStaticWaybill);
    }

    /**
     * 得到OTM的运单
     *
     * @return
     */
    @Override
    public List<StaticWaybill> getStaticOTMWayBillByOrder(int intCount) {
        EntityWrapper<StaticWaybill> ewStaticWaybill = new EntityWrapper<>();
        ewStaticWaybill.eq("deal_status", 0);
        ewStaticWaybill.orderBy("gmt_modify");
        if (intCount > 0) {
            ewStaticWaybill.last("limit " + intCount);
        }
        return baseMapper.getStaticOTMWayBillByOrder(ewStaticWaybill);
    }

    /**
     * 得到没有标准价的运单
     *
     * @param intCount
     * @return
     */
    @Override
    public List<StaticWaybill> getNoStandPriceWaybill(int intCount) {
        EntityWrapper<StaticWaybill> ewStaticWaybill = new EntityWrapper<>();
        ewStaticWaybill.orderBy("gmt_modify");
        if (intCount > 0) {
            ewStaticWaybill.last("limit " + intCount);
        }
        return baseMapper.getNoStandPriceWaybill(ewStaticWaybill);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void syncWaybillToComposite() {
        List<StaticWaybill> noExistWaybillCompositeList = baseMapper.getNoExistWaybillComposite();
        if (!CollectionUtils.isEmpty(noExistWaybillCompositeList)) {
            List<StaticWaybillComposite> insertList = Lists.newArrayList();
            noExistWaybillCompositeList.forEach(waybill -> {
                EntityWrapper<StaticWaybillComposite> ew = new EntityWrapper<>();
                ew.eq("sys_waybill_id", waybill.getSysWaybillId());
                int count = waybillCompositeService.selectCount(ew);
                if (count <= 0) {
                    StaticWaybillComposite composite = new StaticWaybillComposite();
                    BeanUtils.copyProperties(waybill, composite);
                    // 车型
                    composite.setWaybillCarTypeName(waybill.getCarTypeName());
                    // 数量
                    composite.setShipNum(waybill.getNum());
                    // 运单状态
                    composite.setWaybillStatus(waybill.getStatus());
                    composite.setWaybillStartAddr(waybill.getStartAddr());
                    composite.setWaybillEndAddr(waybill.getEndAddr());
                    composite.setWaybillEndProvince(waybill.getEndProvince());
                    insertList.add(composite);
                }
            });
            if (!CollectionUtils.isEmpty(insertList)) {
                waybillCompositeService.insertBatch(insertList);
            }
        }
    }
}
